<%@page import="java.util.ArrayList, model.Course, model.Session, database.DBHandler, servlets.AServlet"%>
<%@page import="java.sql.Connection"%>
<%@include file="/WEB-INF/JSP-Files/headline.jsp" %>
<div id="content">
	<%if (request.getSession().getAttribute("isAdmin").equals(true)){ %>
		<a href="#" onclick="javascript:window.open('/skiryanoandspuzir/Courses?get=addCourse','addCourse', 'width=310,height=300,scrollbars=no')">Add Course</a>
	<%}

	  if (request.getSession(true).getAttribute("CourseRegistrationError") != null){ %>
		<span class="errorMessage"><%= request.getSession(true).getAttribute("CourseRegistrationError").toString() %></span>
		<% request.getSession(true).removeAttribute("CourseRegistrationError");
	  } 
	  if (request.getSession(true).getAttribute("SuccessMessage") != null){ %>
		<span class="successMessage"><%= request.getSession(true).getAttribute("SuccessMessage").toString() %></span>
		<% request.getSession(true).removeAttribute("SuccessMessage");
	  } %>  
	<% if (request.getSession().getAttribute("isAdmin").equals(false)){ %>
		<%if (request.getSession().getAttribute("coursesFilter")!= null && 
				request.getSession().getAttribute("coursesFilter").equals(true)){ %>
			<a href="/skiryanoandspuzir/Courses?showAvailable=false">Show all courses</a>
		<%} else { %>
			<a href="/skiryanoandspuzir/Courses?showAvailable=true">Show only courses available to you</a>
		<%} %>
	<%} %>
	<table class="matrix">
		<tr>
			<th>&nbsp;</th>
			<th>Course Name</th><th>Description</th><th>Points</th><th>Capacity</th>
			<th>Registered</th><th>Group Number</th><th>Sessions list</th>
		</tr>
		<% ArrayList<Course> courses = (ArrayList<Course>)request.getSession().getAttribute("coursesData");
		String[] days = {"Sunday", "Monday", "Tuesday", "Wednsday", "Thursday", "Friday", "Saturday"};
		Connection con = DBHandler.getHandler().getLocalCon();
		ArrayList<String> userCourses =  DBHandler.getHandler().getUser((String)request.getSession().getAttribute("LoginName"), con).getCourses();
		AServlet.finishTransaction(con, false);
		DBHandler.getHandler().returnLocalCon(con); 
		for (Course c : courses) { %>
			<tr>
				<% if (request.getSession().getAttribute("isAdmin").equals(true)){ 
						if (request.getSession().getAttribute("LoginName").equals(c.getCreator())){ %>
							<td><a href="/skiryanoandspuzir/Courses?deleteTarget=<%= c.getCourseName() %>"><img src="Images/delete_icon.png" alt="delete" /></a></td>
						<% }else{ %>
							<td>&nbsp;</td>
						<% } 
					} else { %>
				<td>
					<%if(userCourses.contains(c.getCourseName())){ %>
						<a href="/skiryanoandspuzir/Courses?unRegisterTarget=<%= c.getCourseName() %>"><img src="Images/delete_icon.png" alt="unRegister" /></a>
					<%} else { %>
						<a href="/skiryanoandspuzir/Courses?registerTarget=<%= c.getCourseName() %>"><img src="Images/icon_plus.gif" alt="register" /></a>
					<% } %>
				</td>
				<% } %>
				<td> <%= c.getCourseName() %> </td>
				<td> <%= c.getDescription() %> </td>
				<td> <%= c.numOfpts() %> </td>
				<td> <%= c.getCapacity() %> </td>
				<td> <%= c.getNumOfRegistered() %> </td>
				<td> <%= c.getGroupNum() %> </td>
				<td>
					<ul>
					<% for(Session courseSession : c.getSessions()){%>
						<li> <%= days[courseSession.getDay()-1] %>, <%= courseSession.getStart() %>, <%= courseSession.getEnd() %></li>
					<% } %>
					</ul>
				</td>
			</tr>
		<% } %>
	</table>
</div>
</body>
</html>